package cn.iocoder.yudao.framework.common.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 *  通用性别枚举
 * @Author LiZhiMin
 * @Date 2024/8/30 15:57
 */
@Getter
@AllArgsConstructor
public enum CommonGenderEnum {
    MAN("0", "男"),
    WOMAN("1", "女"),
    UN_KNOWN("2", "未知"),
    MEN_AND_WOMEN("999", "塑料袋");

    private final String code;
    private final String gender;

    private static final Map<String, CommonGenderEnum> CODE_TO_ENUM;

    static {
        Map<String, CommonGenderEnum> map = new HashMap<>();
        for (CommonGenderEnum genderEnum : values()) {
            map.put(genderEnum.getCode(), genderEnum);
        }
        CODE_TO_ENUM = Collections.unmodifiableMap(map);
    }

    /**
     * 根据性别代码获取对应的性别名称
     * @param code 性别代码
     * @return 性别名称，如果未找到则返回"未知性别"
     */
    public static String getGenderByCode(String code) {
        return Optional.ofNullable(CODE_TO_ENUM.get(code))
                .map(CommonGenderEnum::getGender)
                .orElse("未知性别");
    }
}
